package com.lianchuang.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CiliBaUtil {
	
	public static int max_count = 0;//获取最多链接数
	
	
	
	public static Map<String, Map<String,String>> getDownLoadUrlByMap(String videoName) throws Exception {
		Map<String, Map<String,String>> map = new HashMap<String, Map<String,String>>();
		String url = "https://www.ciliba.org/s/" + videoName + ".html";
		Document doc = StaticResourceDownload.getDoc(url);
		Elements eles = doc.getElementsByClass("item-title");
		Map<String,String> mList = new HashMap<String,String>();// 磁力地址
		Map<String,String> tList = new HashMap<String,String>();// 迅雷地址
		if (eles != null) {
			Iterator<Element> itemItera = eles.iterator();
			int count = 0;
			while (itemItera.hasNext()) {// 遍历每一个搜索结果
				count++;
				Element item = itemItera.next();
				Element aElem = item.getElementsByTag("a").get(0);
				String subUrl = aElem.attr("href");
				String subName = aElem.text();
				Document subDoc = StaticResourceDownload.getDoc(subUrl);
				Element detailEle = subDoc.getElementsByClass("fileDetail")
						.get(0);
				Elements detailAeles = detailEle.getElementsByTag("a");
				Iterator<Element> aIterm = detailAeles.iterator();
				while (aIterm.hasNext()) {
					Element a = aIterm.next();
					String durl = a.attr("href");
					if (durl.startsWith("m")) {
						mList.put(subName,durl);
					}
					if (durl.startsWith("t")) {
						tList.put(subName,durl);
					}
				}
				if (count > max_count) {
					break;
				}
			}
		}
		map.put("m", mList);
		map.put("t", tList);
		return map;
	}
	
	public static Map<String, List<String>> getDownLoadUrl(String videoName) throws Exception {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		String url = "https://www.ciliba.org/s/" + videoName + ".html";
		Document doc = StaticResourceDownload.getDoc(url);
		Elements eles = doc.getElementsByClass("item-title");
		List<String> mList = new ArrayList<String>();// 磁力地址
		List<String> tList = new ArrayList<String>();// 迅雷地址
		if (eles != null) {
			try {
				Iterator<Element> itemItera = eles.iterator();
				int count = 0;
				while (itemItera.hasNext()) {// 遍历每一个搜索结果
					count++;
					Element item = itemItera.next();
					Element aElem = item.getElementsByTag("a").get(0);
					String subUrl = aElem.attr("href");
					Document subDoc = StaticResourceDownload.getDoc(subUrl);
					Element detailEle = subDoc.getElementsByClass("fileDetail")
							.get(0);
					Elements detailAeles = detailEle.getElementsByTag("a");
					Iterator<Element> aIterm = detailAeles.iterator();
					while (aIterm.hasNext()) {
						Element a = aIterm.next();
						String durl = a.attr("href");
						if (durl.startsWith("m")) {
							mList.add(durl);
						}
						if (durl.startsWith("t")) {
							tList.add(durl);
						}
					}
					if (count >= max_count) {
						break;
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		map.put("m", mList);
		map.put("t", tList);
		return map;
	}
	public static void showMap(Map<String,String> map,StringBuffer sb){
		if(map.size() == 0){
			return;
		}
		for(Map.Entry<String, String> mapEntry : map.entrySet()){
			sb.append(mapEntry.getKey().replaceAll("www.", "").replaceAll(".cn", "").replace(".com", "")+"\r\n下载地址:"+mapEntry.getValue()+"\r\n ");
		}
	}

	public static String getMsg(String videoName) throws Exception {
		Map<String,Map<String,String>> map = getDownLoadUrlByMap(videoName);
		StringBuffer sb = new StringBuffer("");
		
		Map<String,String> mMap = map.get("m");
		showMap(mMap,sb);
		return sb.toString();
	}
	public static void main(String[] args) throws Exception {
		System.out.println(getMsg("蜘蛛侠3"));
	}
}
